package com.cuitrip.business.home.arrange.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import com.cuitrip.service.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by ak on 15/10/16.
 */
public class MonthDateAdapter extends BaseAdapter {


    private Context mContext;
    private List<Long> mAvailableDate = new ArrayList<>();
    private List<Long> mBookedDate = new ArrayList<>();
    private boolean mIsFinder;

    public MonthDateAdapter(Context context, boolean isFinder){
        this.mContext = context;
        this.mIsFinder = isFinder;
    }

    public void setBookDateList(List<Long> bookDateList) {
        mBookedDate.clear();
        mBookedDate.addAll(bookDateList);
    }

    public void setAvailableDateList(List<Long> availableDateList) {
        mAvailableDate.clear();
        mAvailableDate.addAll(availableDateList);
    }

    private Calendar calendar = Calendar.getInstance();


    public void setCalendar(Calendar cal) {
        this.calendar.setTimeInMillis(cal.getTimeInMillis());
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+000"));
    }

    @Override
    public int getCount() {
        return 42; //按照最多6个星期计算
    }

    @Override
    public Integer getItem(int i) {
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return i + 2 - dayOfWeek;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (view == null || !(view instanceof CheckedTextView)) {
            view = View.inflate(mContext, R.layout.ct_date_item, null);
        }
        CheckedTextView tv = (CheckedTextView) view;
        int date = getItem(position);
        if (date < 1 || date > calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            tv.setText("");
            tv.setEnabled(false);
            tv.setVisibility(View.INVISIBLE);
        } else {
            calendar.set(Calendar.DAY_OF_MONTH, date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            long start = calendar.getTimeInMillis();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            long end = calendar.getTimeInMillis();

            tv.setText(String.valueOf(date));
            view.setTag(Long.valueOf(start));
            tv.setVisibility(View.VISIBLE);
            tv.setEnabled(true);
            tv.setChecked(false);

            boolean matchAvaliable = false;
            if (mAvailableDate != null && !mAvailableDate.isEmpty()) {
                for (long time : mAvailableDate) {
                    if (time >= start && time < end) {
                        tv.setEnabled(true);
                        tv.setChecked(true);
                        matchAvaliable = true;
                        break;
                        //avaliable
                    }
                }
            }
            if (!matchAvaliable) {
                Calendar today = Calendar.getInstance();
                today.setTimeZone(calendar.getTimeZone());
                today.set(Calendar.HOUR_OF_DAY, 0);
                today.set(Calendar.MINUTE, 0);
                today.set(Calendar.SECOND, 0);
                today.set(Calendar.MILLISECOND, 0);

                Calendar maxDate = Calendar.getInstance();
                maxDate.setTimeZone(calendar.getTimeZone());
                maxDate.set(Calendar.HOUR_OF_DAY, 23);
                maxDate.set(Calendar.MINUTE, 59);
                maxDate.set(Calendar.SECOND, 59);
                maxDate.set(Calendar.MILLISECOND, 999);
                maxDate.add(Calendar.DAY_OF_YEAR, 60);

                if (mIsFinder) {
                    //当天之前不能被选中
                    if (start <= today.getTimeInMillis()) {
                        tv.setEnabled(false);
                        tv.setChecked(false);
                    }else if (start >= maxDate.getTimeInMillis()){
                        tv.setEnabled(false);
                        tv.setChecked(false);
                    }
                } else {
                    tv.setEnabled(false);
                    tv.setChecked(false);
                }
            }

            if (mBookedDate != null && !mBookedDate.isEmpty()) {
                for (long time : mBookedDate) {
                    if (time >= start && time < end) {
                        tv.setEnabled(false);
                        tv.setChecked(true);
                        break;
                        //booked
                    }
                }
            }
        }

        if (mIsFinder && view.isEnabled()) {
            view.setOnClickListener(onDateSelectedListener);
        }
        return view;
    }



    private View.OnClickListener onDateSelectedListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (view.getTag() == null || !(view.getTag() instanceof Long)) {
                return;
            }
            Long time = (Long) view.getTag();
            if (mAvailableDate.contains(time)) {
                mAvailableDate.remove(time);
                ((CheckedTextView) view).setChecked(false);
                if (onSelectChangeCallBack != null){
                    onSelectChangeCallBack.onSelectRemove(time);
                }
            } else {
                mAvailableDate.add(time);
                ((CheckedTextView) view).setChecked(true);
                if (onSelectChangeCallBack != null){
                    onSelectChangeCallBack.onSelectAdd(time);
                }
            }

        }
    };

    private OnSelectChangeCallBack onSelectChangeCallBack;

    public void setOnSelectChangeCallBack(OnSelectChangeCallBack onSelectChangeCallBack){
        this.onSelectChangeCallBack = onSelectChangeCallBack;
    }

    public List<Long> getmAvailableDate() {
        return mAvailableDate;
    }

    public List<Long> getmBookedDate() {
        return mBookedDate;
    }

//    public void setTimeZone(String timeZone) {
//        this.timeZone = timeZone;
//        this.calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
//        this.notifyDataSetChanged();
//    }

    public interface OnSelectChangeCallBack{

        public void onSelectRemove(Long time);

        public void onSelectAdd(Long time);

    }

}
